# -*- coding: utf-8 -*-
# @Time : 2021/2/2 下午 2:03
# @Author : lijunlong
# @Email : 729619494@qq.com
# @File : tootls.py
# @Project : python

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class Tools():
    def __init__(self,driver):
        self.driver = driver

    def table_check_and_choice(self,text,check,choice=1,firstline=2,lines_locator=(By.TAG_NAME,"tr")):
        """
        :param text: 你要查询的文本
        :param lines_locator: tbody下所有行的locator
        :param check: 你需要查询的tbody在第几列
        :param choice: 你要勾选的项在第几列
        :param firstline: 你那个表在tbody是第几行开始的
        :return:
        """
        lines = self.driver.find_elements(*lines_locator)[firstline:]
        for i in range(len(lines)):
            cell = lines[i].find_element(By.CSS_SELECTOR,f"tr>td:nth-child({check})").text
            # 找到第check列单元格内部的文本
            if cell == text:
                lines[i].find_element(By.CSS_SELECTOR,f"tr>td:nth-child({choice})>input").click()

    def table_check(self, text, check, firstline=2, lines_locator=(By.TAG_NAME, "tr")):
        """
        有模糊匹配的就返回ok，否者 返回bad
        """
        lines = self.driver.find_elements(*lines_locator)[firstline:]
        for i in range(len(lines)):
            cell = lines[i].find_element(By.CSS_SELECTOR, f"tr>td:nth-child({check})").text
            # 找到第check列单元格内部的文本
            if text in cell:
                return "ok"
            break


    def selector(self,text,select_loctor):
        """
        这是个选择器，只需要输入要选的内容和选择框地址就行了
        """
        select1 = self.driver.find_element(*select_loctor)
        select2 = Select(select1)
        select2.select_by_visible_text(text)